package com.fesc.trade.demo.request;

import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fesc.trade.demo.model.BaseModel;
import com.fesc.trade.demo.util.AssertUtil;

/**
 * 商户查询接口
 * @author prd-wfc
 * @version $Id: MerchantQueryRequest.java, v 0.1 2016年8月18日 下午7:54:18 prd-wfc Exp $
 */
public class MerchantQueryRequest extends BaseRequest {

    /** uid */
    private static final long   serialVersionUID = 1L;

    private BizContent          bizContent       = new BizContent();

    private Map<String, String> paraMap          = new TreeMap<String, String>();

    @Override
    public void validate() {

        super.validate();

        AssertUtil.assertNotBlank(bizContent.externalId + bizContent.merchantId, "external_id and merchant_id should not be NULL at the same time!");

    }

    public String getExternalId() {
        return bizContent.externalId;
    }

    public MerchantQueryRequest setExternalId(String externalId) {
        bizContent.externalId = externalId;
        return this;
    }

    public String getMerchantId() {
        return bizContent.merchantId;
    }

    public MerchantQueryRequest setMerchantId(String merchantId) {
        bizContent.merchantId = merchantId;
        return this;
    }

    public String getExternalInfo() {
        return bizContent.externalInfo;
    }

    public MerchantQueryRequest setExternalInfo(String externalInfo) {
        bizContent.externalInfo = externalInfo;
        return this;
    }

    public Map<String, String> buildParaMap() {
        super.setBizContent(bizContent.buildBizContentStr());

        if (StringUtils.isNotBlank(acquirerId)) {
            paraMap.put("acquirer_id", acquirerId);
        }
        if (StringUtils.isNotBlank(signType)) {
            paraMap.put("sign_type", signType);
        }
        if (StringUtils.isNotBlank(timestamp)) {
            paraMap.put("timestamp", timestamp);
        }
        if (StringUtils.isNotBlank(appAuthToken)) {
            paraMap.put("app_auth_token", appAuthToken);
        }
        if (StringUtils.isNotBlank(walletType)) {
            paraMap.put("wallet_type", walletType);
        }
        if (StringUtils.isNotBlank(super.bizContent)) {
            paraMap.put("biz_content", super.bizContent);
        }

        return paraMap;

    }

    /** 业务参数 */
    private static class BizContent extends BaseModel {

        /**  */
        private static final long serialVersionUID = 1L;

        // (特殊可选)商户外部编号，一个受理机构下下面唯一
        @JSONField(name = "external_id")
        private String            externalId;

        // (特殊可选)商户标识号
        @JSONField(name = "merchant_id")
        private String            merchantId;

        // 扩展信息
        @JSONField(name = "external_info")
        private String            externalInfo;

        public String buildBizContentStr() {

            Map<String, Object> bizParaMap = new TreeMap<String, Object>();

            if (StringUtils.isNotBlank(externalId)) {
                bizParaMap.put("external_id", externalId);
            }
            if (StringUtils.isNotBlank(merchantId)) {
                bizParaMap.put("merchant_id", merchantId);
            }
            if (StringUtils.isNotBlank(externalInfo)) {
                bizParaMap.put("external_info", externalInfo);
            }

            return JSON.toJSONString(bizParaMap);
        }
    }

}
